package com.yuyou.train.lesson.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.yuyou.train.lesson.service.ILessonClassService;
import com.yuyou.train.lesson.service.ILessonResourceRuleRelationService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.lesson.vo.req.ReqLessonRel;
import com.yuyou.train.lesson.vo.req.ReqLessonRelFrame;
import com.yuyou.train.lesson.vo.req.ReqLessonUpdate;
import com.yuyou.train.service.api.lesson.dto.LessonDetailDTO;
import com.yuyou.train.service.api.lesson.dto.LessonTreeDTO;
import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleFrame;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleRelation;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;

@Validated
@RestController
@RequestMapping(value="/api/lesson")
public class LessonController extends BaseController{

	
	@Autowired
	private ILessonService lessonService;
	
	@Autowired
	private ILessonClassService lessonClassService;
	
	@Autowired
	private ILessonResourceRuleRelationService lesResRulRelService;

	
	@ApiOperation(value="新增课程")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "parentId", value = "父id", paramType="query", required = true),
		@ApiImplicitParam(name = "name", value = "名字", paramType="query",required = true)}
		)
	@RequestMapping(value="/addLesson",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<Lesson>> addLesson(
		@NotBlank (message="parentId不能为空") String parentId,
		@NotBlank (message="name不能为空") 
		@Length(max=20,message="课程名字(name)长度过长") String name) throws Exception{
		ResBoolSimpleInfo<Lesson> res = lessonService.addLesson(parentId, name);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="更新课程名称")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "课程 id", paramType="query", required = true),
			@ApiImplicitParam(name = "name", value = "课程名字", paramType="query",required = true)}
	)
	@RequestMapping(value="/updateLessonName",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<Lesson>> updateLessonName(
			@NotBlank (message="id不能为空") String id,
			@NotBlank (message="name不能为空") 
			@Length(max=20,message="课程名字(name)长度过长") String name){
		ResBoolSimpleInfo<Lesson> res=lessonService.updateLessonName(id,name);
		return ReturnResult.success(res);
	}
	
	
	@ApiOperation(value="更新课程")
	@RequestMapping(value="/updateLesson",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<Lesson>> updateLesson(@Validated @ModelAttribute ReqLessonUpdate reqLessonUpdate){	
		ResBoolSimpleInfo<Lesson> res = lessonService.updateLessonAndRel(reqLessonUpdate);
		return ReturnResult.success(res);
	}


	
	@ApiOperation(value="删除课程")
	@ApiImplicitParam(name = "id", value = "课程ID", paramType="query", required = true)
	@RequestMapping(value="/delLesson",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> delLesson(
			@NotBlank (message="id不能为空") String id) throws Exception{	
		ResBoolSimpleInfo<String> res=lessonService.delLesson(id);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="课程详情")
	@ApiImplicitParam(name = "id", value = "课程ID", paramType="query", required = true)
	@RequestMapping(value="/detailLesson",method=RequestMethod.POST)
	public ReturnResult<LessonDetailDTO> detailLesson(@NotBlank (message="id不能为空") String id){
		LessonDetailDTO res = lessonService.detailLesson(id);
		return ReturnResult.success(res);
	}
	
		
	@ApiOperation(value="根据班级获取课程树")
	@ApiImplicitParam(name = "classId", value = "班级ID", paramType="query", required = true)
	@RequestMapping(value="/lessonTree",method=RequestMethod.POST)
	public ReturnResult<List<LessonTreeDTO>> lessonTree(@NotBlank (message="classId不能为空") String classId){
		List<LessonTreeDTO> res = lessonService.lessonTree(classId);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="设置课程，资源，规则，帧，的关系")
	@ApiImplicitParam(name = "reqLessonRelStr", 
	value = "JSON格式（reqLessonRelStr={\"lesResRulFra\":[{\"resourceQuestionId\":1,\"second\":1},{\"resourceQuestionId\":2,\"second\":2}],\"lessonId\":\"fdd9e2f6b0694d16bbd7d0a0680eed18\",\"resourceId\":\"1\",\"ruleId\":1}）", paramType="query", required = true)
	@RequestMapping(value="/setRelation",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> setRelation(	@NotBlank (message="reqLessonRelStr不能为空") String reqLessonRelStr){
		ResBoolSimpleInfo<String> res =new ResBoolSimpleInfo<String>();		
		ReqLessonRel reqLessonRel=JSON.parseObject(reqLessonRelStr,ReqLessonRel.class);				
		
		ResBoolSimpleInfo<String> valRes=ReqLessonRel.validate(reqLessonRel);
		if(!valRes.isSuccess()){
			res.setSuccess(false);
			res.setMessage(valRes.getMessage());
			return ReturnResult.success(res);
		}
		
		LessonResourceRuleRelation lrrr=new LessonResourceRuleRelation();
		lrrr.setLessonId(reqLessonRel.getLessonId());
		lrrr.setResourceId(reqLessonRel.getResourceId());
		lrrr.setRuleId(reqLessonRel.getRuleId());
		
		if(reqLessonRel.getLesResRulFra()!=null){
			List<LessonResourceRuleFrame> list=new ArrayList<LessonResourceRuleFrame>();
			for(ReqLessonRelFrame frame:reqLessonRel.getLesResRulFra()){
				if(frame.getSecond()==null|| StringUtils.isEmpty(frame.getResourceQuestionId())){
					continue;
				}
				LessonResourceRuleFrame lrrf=new LessonResourceRuleFrame();
				lrrf.setSecond(frame.getSecond());
				lrrf.setResourceQuestionId(frame.getResourceQuestionId());
				list.add(lrrf);
			}
			res = lesResRulRelService.setRelation(lrrr,list);	
		}else{
			 res = lesResRulRelService.setRelation(lrrr,null);	
		}
		res.setData(reqLessonRelStr);
		return ReturnResult.success(res);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
